package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.DatabaseOperations;
import database.Job;
import database.JobState;

import java.sql.PreparedStatement;

public class AddJob extends HttpServlet {

	Connection conn = null;
	Statement stmt = null;
	private DatabaseOperations databaseOperations;
	private Job job;
	
	
	/** Process the HTTP Get request */
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		// get the variables entered in the form

		databaseOperations = new DatabaseOperations();
		
		
		int ida = Integer.parseInt(req.getParameter("id"));
		String fname = req.getParameter("firstname");
		String lname = req.getParameter("lastname");
		String country = req.getParameter("country");
		String town = req.getParameter("town");
		
		job = new Job();
		job.setDestAddress(town);
		job.setDestName(fname);
		job.setExpAdress(country);
		job.setExpName(lname);
		job.setJobState(JobState.NOT_ASSIGNED);
		job.setOrderId(ida);
		job.setTransporterName("transporterName");
		databaseOperations.addJob(job);
//		try {
//			// Get a Connection to the database
////			conn = DatabaseConection.getConnectionToDB();
//			// Add the data into the database
//			String sql = "insert into authors values (?,?,?,?,?)";
//			PreparedStatement pst = conn.prepareStatement(sql);
//			pst.setInt(1, ida);
//			pst.setString(2, fname);
//			pst.setString(3, lname);
//			pst.setString(4, country);
//			pst.setString(5, town);
//			
//			pst.executeUpdate();
//			// show that the new account has been created
//			pst.close();
//			res.sendRedirect("http://localhost:8080/RoadRunner/");
//		} catch (ClassNotFoundException e) {
//			out.println("Couldn't load database driver: " + e.getMessage());
//		} catch (SQLException e) {
//			out.println("SQLException caught: " + e.getMessage());
//		} catch (Exception e) {
//			out.println(e);
//		} finally {
//			// Always close the database connection.
//			try {
//				if (conn != null)
//					conn.close();
//			} catch (SQLException ignored) {
//				out.println(ignored);
//			}
//		}
	}
}
